<template>
  <div class="merchant-detail" v-if="merchant">
    <!-- 商家头部信息 -->
    <section class="merchant-header">
      <div class="container">
        <div class="header-content">
          <div class="merchant-image">
            <img :src="merchant.image" :alt="merchant.name" />
          </div>
          <div class="merchant-info">
            <div class="merchant-badges">
              <el-tag type="primary" size="large">{{ merchant.category }}</el-tag>
              <el-tag v-if="merchant.featured" type="warning" size="large">精选商家</el-tag>
            </div>
            <h1 class="merchant-name">{{ merchant.name }}</h1>
            <p class="merchant-description">{{ merchant.description }}</p>
            <div class="merchant-meta">
              <div class="meta-item">
                <el-icon><Star /></el-icon>
                <span class="rating">{{ merchant.rating }}</span>
                <span class="rating-text">评分</span>
              </div>
              <div class="meta-item">
                <el-icon><Location /></el-icon>
                <span>{{ merchant.location }}</span>
              </div>
              <div class="meta-item">
                <el-icon><Calendar /></el-icon>
                <span>{{ merchant.established }}年成立</span>
              </div>
            </div>
            <div class="contact-actions">
              <el-button type="primary" size="large" @click="showContactDialog = true">
                <el-icon><Phone /></el-icon>
                立即联系
              </el-button>
              <el-button size="large" @click="$router.push('/contact')">
                <el-icon><Message /></el-icon>
                申请合作
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 详细信息 -->
    <section class="merchant-details section">
      <div class="container">
        <div class="details-grid">
          <!-- 主营产品 -->
          <div class="detail-card card">
            <h3><el-icon><Goods /></el-icon>主营产品</h3>
            <div class="specialties-list">
              <el-tag
                v-for="specialty in merchant.specialties"
                :key="specialty"
                size="large"
                type="info"
              >
                {{ specialty }}
              </el-tag>
            </div>
          </div>

          <!-- 价格信息 -->
          <div class="detail-card card">
            <h3><el-icon><Money /></el-icon>价格信息</h3>
            <div class="price-info">
              <div class="price-item">
                <span class="label">价格区间：</span>
                <span class="value">{{ merchant.priceRange }}</span>
              </div>
              <div class="price-item">
                <span class="label">起订量：</span>
                <span class="value">{{ merchant.minOrder }}</span>
              </div>
            </div>
          </div>

          <!-- 企业优势 -->
          <div class="detail-card card full-width">
            <h3><el-icon><Trophy /></el-icon>企业优势</h3>
            <div class="advantages-list">
              <div
                v-for="advantage in merchant.advantages"
                :key="advantage"
                class="advantage-item"
              >
                <el-icon class="check-icon"><Check /></el-icon>
                <span>{{ advantage }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 联系方式对话框 -->
    <el-dialog
      v-model="showContactDialog"
      title="联系方式"
      width="500px"
      center
    >
      <div class="contact-info">
        <div class="contact-item">
          <el-icon><Phone /></el-icon>
          <div>
            <div class="contact-label">联系电话</div>
            <div class="contact-value">{{ merchant.contact.phone }}</div>
          </div>
        </div>
        <div class="contact-item">
          <el-icon><Message /></el-icon>
          <div>
            <div class="contact-label">邮箱地址</div>
            <div class="contact-value">{{ merchant.contact.email }}</div>
          </div>
        </div>
        <div class="contact-item">
          <el-icon><ChatDotRound /></el-icon>
          <div>
            <div class="contact-label">微信号</div>
            <div class="contact-value">{{ merchant.contact.wechat }}</div>
          </div>
        </div>
      </div>
      <template #footer>
        <el-button @click="showContactDialog = false">关闭</el-button>
        <el-button type="primary" @click="copyContact">复制联系方式</el-button>
      </template>
    </el-dialog>
  </div>

  <!-- 加载状态 -->
  <div v-else class="loading-state">
    <el-skeleton :rows="8" animated />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  Star, Location, Calendar, Phone, Message, Goods, Money, Trophy, Check, ChatDotRound
} from '@element-plus/icons-vue'
import merchantsData from '../data/merchants.json'

interface Merchant {
  id: number
  name: string
  category: string
  description: string
  image: string
  rating: number
  location: string
  established: string
  specialties: string[]
  contact: {
    phone: string
    email: string
    wechat: string
  }
  advantages: string[]
  minOrder: string
  priceRange: string
  featured: boolean
}

const route = useRoute()
const router = useRouter()
const merchant = ref<Merchant | null>(null)
const showContactDialog = ref(false)

const copyContact = () => {
  if (!merchant.value) return
  
  const contactText = `
商家：${merchant.value.name}
电话：${merchant.value.contact.phone}
邮箱：${merchant.value.contact.email}
微信：${merchant.value.contact.wechat}
  `.trim()
  
  navigator.clipboard.writeText(contactText).then(() => {
    ElMessage.success('联系方式已复制到剪贴板')
    showContactDialog.value = false
  }).catch(() => {
    ElMessage.error('复制失败，请手动复制')
  })
}

onMounted(() => {
  const merchantId = parseInt(route.params.id as string)
  const foundMerchant = merchantsData.find(m => m.id === merchantId)
  
  if (foundMerchant) {
    merchant.value = foundMerchant as Merchant
  } else {
    ElMessage.error('商家不存在')
    router.push('/merchants')
  }
})
</script>

<style scoped>
.merchant-detail {
  min-height: 100vh;
  background: var(--accent-color);
}

/* 商家头部 */
.merchant-header {
  background: white;
  padding: 60px 0;
}

.header-content {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 40px;
  align-items: start;
}

.merchant-image {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow-medium);
}

.merchant-image img {
  width: 100%;
  height: 300px;
  object-fit: cover;
}

.merchant-badges {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.merchant-name {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 15px;
}

.merchant-description {
  font-size: 1.125rem;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 30px;
}

.merchant-meta {
  display: flex;
  gap: 30px;
  margin-bottom: 40px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
}

.rating {
  font-weight: 600;
  color: #f39c12;
  margin-right: 5px;
}

.contact-actions {
  display: flex;
  gap: 15px;
}

/* 详细信息 */
.merchant-details {
  padding: 60px 0;
}

.details-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 30px;
}

.detail-card {
  padding: 30px;
  background: white;
}

.detail-card.full-width {
  grid-column: 1 / -1;
}

.detail-card h3 {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 20px;
}

.specialties-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.price-info {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.price-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.label {
  color: var(--text-secondary);
}

.value {
  font-weight: 600;
  color: var(--text-primary);
}

.advantages-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 15px;
}

.advantage-item {
  display: flex;
  align-items: center;
  gap: 10px;
}

.check-icon {
  color: #67c23a;
  font-size: 18px;
}

/* 联系方式对话框 */
.contact-info {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 15px;
  background: var(--accent-color);
  border-radius: 8px;
}

.contact-label {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.contact-value {
  font-weight: 600;
  color: var(--text-primary);
}

.loading-state {
  padding: 60px 20px;
  max-width: 1200px;
  margin: 0 auto;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    grid-template-columns: 1fr;
    gap: 30px;
    text-align: center;
  }
  
  .merchant-image {
    max-width: 300px;
    margin: 0 auto;
  }
  
  .merchant-meta {
    justify-content: center;
    flex-wrap: wrap;
  }
  
  .contact-actions {
    justify-content: center;
    flex-wrap: wrap;
  }
  
  .details-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
}
</style>
